package com.pay;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;

import com.itrus.util.sign.RSAWithSoftware;
import com.jfinal.core.Controller;
import com.pay.dinpay.client.serivce.DinpayService;
import com.pay.yeepay.client.service.YeepayService;
import com.pay.yeepay.client.service.yeepayTimer;
import com.pay.yeepay.server.Configuration;
import com.pay.yeepay.server.PaymentForOnlineService;
import com.tool.CryptTool;
import com.vo.Gateway;
import com.vo.Order;
import com.vo.OrdersNew;
import com.vo.Person;
import com.vo.PersonGateway;
import com.vo.System_interface;

public class hdreturn extends Controller {

	public void node() {
		try {
			getRequest().setCharacterEncoding("utf-8");

			String pay_key = getPara("pay_key");
			String pay_key_self = Configuration.getInstance().getValue("pay_key");
			if(!pay_key_self.equals(pay_key)){
				System.out.println("非法请求!" + new Date());
				renderHtml("非法请求!");
			}
			
			String pay_channel = getPara("pay_channel");
			String pay_type = getPara("pay_type");

			String merchant_code = getPara("merchant_code");
			String sign_type = getPara("sign_type");
			String order_no = getPara("order_no");
			String order_amount = getPara("order_amount");
			String order_time = getPara("order_time");
			
			String dinpaySign= getPara("dinpaySign");
			String notify_type = getPara("notify_type");
			String notify_id = getPara("notify_id");
			String trade_time= getPara("trade_time");
			String extra_return_param = getPara("extra_return_param");
			String trade_no = getPara("trade_no");
			String trade_status = getPara("trade_status");
			String bank_seq_no= getPara("bank_seq_no");
			
			OrdersNew order = OrdersNew.dao.findById(order_no);
			if(order == null){
				renderHtml("参数有误，没有此订单！");
			}
			
			boolean flag = true;
			if( null != order.getStr("trade_status") ){
				flag = false;
			}
			
			order.set("notify_type", notify_type);
			order.set("notify_id", notify_id);
			order.set("trade_no", trade_no);
			order.set("trade_time", trade_time);
//			order.set("extra_return_param", extra_return_param);
			order.set("trade_status", trade_status);
			order.set("bank_seq_no", bank_seq_no);
			order.set("notify_hmac", dinpaySign);
			
			// 判断支付通道和支付方式
			if("dinpay".equals(pay_channel)){ // 智付
				if("b2c".equals(pay_type)){ // 网银
					dinpayB2C(order, flag);
				}
			}

			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 智付B2C异步通知处理 
	 * @param order 
	 * @param flag 是否智付第一次返回的标识
	 */
	public void dinpayB2C(OrdersNew order, boolean flag){
		// 验签
		if(flag){
			}
			StringBuilder signStr = new StringBuilder();
			String bank_seq_no = order.getStr("bank_seq_no");
			if(null != bank_seq_no && !bank_seq_no.equals("")) {
				signStr.append("bank_seq_no=").append(bank_seq_no).append("&");
			}
			String extra_return_param = order.getStr("extra_return_param");
			signStr.append("extra_return_param=").append("undefined").append("&");

//			if(null != extra_return_param && !extra_return_param.equals("")) {
//				signStr.append("extra_return_param=").append(extra_return_param).append("&");
//			}
			signStr.append("interface_version=").append(getPara("interface_version")).append("&");
			signStr.append("merchant_code=").append(getPara("merchant_code")).append("&"); 	
			signStr.append("notify_id=").append(getPara("notify_id")).append("&");	 	
			signStr.append("notify_type=").append(getPara("notify_type")).append("&"); 	
			signStr.append("order_amount=").append(getPara("order_amount")).append("&");
			signStr.append("order_no=").append(getPara("order_no")).append("&");
			signStr.append("order_time=").append(getPara("order_time")).append("&");
			signStr.append("trade_no=").append(getPara("trade_no")).append("&");	
			signStr.append("trade_status=").append(getPara("trade_status")).append("&");
			signStr.append("trade_time=").append(getPara("trade_time"));
			String signInfo =signStr.toString();
			
			boolean result = false;
//			String signtext = "bank_seq_no=91517380078169217533&extra_return_param=undefined&interface_version=V3.0&merchant_code=1111110166&notify_id=97a57d41468b43c7ad0dcac43da17b61&notify_type=offline_notify&order_amount=0.1&order_no=O2018013114249226&order_time=2018-01-31 14:24:53&trade_no=1660644630&trade_status=SUCCESS&trade_time=2018-01-31 14:27:56";
			String dinpaySign = order.getStr("notify_hmac");
//			String tst = "SI2yaSiMz8byfOhzyiccxLoOtDg1A7k5foTJ+xz2F3h7tEMjfp+QrMOHreXAZ9YWGlNK7itQcwDpF9u0b9PKnZhHbSGl7EG3pzCww07vraRn8zsBV+37rAMU8X16i/JSU1JP18FkkuEH0EY9v877j4+ftGlGz1rolBYu6S/w50k="
			String dinpay_public_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCWOq5aHSTvdxGPDKZWSl6wrPpnMHW+8lOgVU71jB2vFGuA6dwa/RpJKnz9zmoGryZlgUmfHANnN0uztkgwb+5mpgmegBbNLuGqqHBpQHo2EsiAhgvgO3VRmWC8DARpzNxknsJTBhkUvZdy4GyrjnUrvsARg4VrFzKDWL0Yu3gunQIDAQAB";		
			try {
				result=RSAWithSoftware.validateSignByPublicKey(signInfo, dinpay_public_key, dinpaySign);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
//			if(result){ // 验签通过，保存支付信息
				order.set("trade_status", order.getStr("trade_status"));
				order.update();
//			}else{
//				renderHtml("交易签名被篡改!");
//				return;
//			}
			
		
		// 返回支付结果到商家
		String keyValue = Configuration.getInstance().getValue("keyValue"); // 商家密钥
		String bank_seq_no_1 = order.getStr("bank_seq_no");
		String input_charset_2 = "UTF-8"; // 固定值
		String merchant_code_3 = order.getInt("merchant_code_4") + "";
		String notify_id_4 = order.getStr("notify_id");
		String notify_type_5 = order.getStr("notify_type");
		String order_amount_6 = order.getBigDecimal("order_amount_6") + "";
		String order_no_7 = order.getStr("order_no_7");
		String order_time_8 = getPara("order_time");
		String trade_no_9 = order.getStr("orderid");
		String trade_status_10 = order.getStr("trade_status");
		String trade_time_11 = getPara("trade_time");
		String hmac = ""; // 交易签名串

		// 获得MD5-HMAC签名
		hmac = PaymentForOnlineService
				.getHmacReturn(bank_seq_no_1, input_charset_2,
						merchant_code_3, notify_id_4, notify_type_5,
						order_amount_6, order_no_7, order_time_8,
						trade_no_9, trade_status_10, trade_time_11,
						keyValue);
		setAttr("bank_seq_no_1", bank_seq_no_1);
		setAttr("input_charset_2", input_charset_2);
		setAttr("merchant_code_3", merchant_code_3);
		setAttr("notify_id_4", notify_id_4);
		setAttr("notify_type_5", notify_type_5);
		setAttr("order_amount_6", order_amount_6);
		setAttr("order_no_7", order_no_7);
		setAttr("order_time_8", order_time_8);
		setAttr("trade_no_9", trade_no_9);
		setAttr("trade_status_10", trade_status_10);
		setAttr("trade_time_11", trade_time_11);
		setAttr("hmac", hmac);
		
		setAttr("notify_url", order.getStr("notify_url_5"));
		setAttr("return_url", order.getStr("return_url_11"));
		
		renderJsp("/WEB-INF/pay/dinpay/b2c/bankReturn.jsp");
	}

}
